const mongoose = require('mongoose');
const afterSalesDemandModel = require('../db/AfterSalesDemand');
const { v4: uuidv4 } = require('uuid');

// 连接数据库
mongoose.connect('mongodb://localhost:27017/project-merage', {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

// 测试数据
const testData = [
  {
    id: uuidv4(),
    name: "20220707需求记录",
    type: "BUG",
    planHour: 800,
    cost: "￥10,0000",
    charge: "是",
    remark: "系统登录功能存在异常，需要修复",
    creator: "韩倩倩",
    createTime: "2022/06/01",
    updateTime: "2022/06/01",
    projectId: "P123123231",
    projectNumber: "P123123231",
    projectName: "CRM系统",
    status: "待处理",
    priority: "高",
    assignee: "张三",
    actualHour: 0,
    description: "用户反馈登录时出现500错误，需要紧急修复",
    attachments: [],
    tags: ["紧急", "登录"]
  },
  {
    id: uuidv4(),
    name: "20220708功能优化需求",
    type: "功能需求",
    planHour: 1200,
    cost: "￥15,0000",
    charge: "否",
    remark: "客户要求增加数据导出功能",
    creator: "李四",
    createTime: "2022/06/02",
    updateTime: "2022/06/02",
    projectId: "P123123231",
    projectNumber: "P123123231",
    projectName: "CRM系统",
    status: "处理中",
    priority: "中",
    assignee: "王五",
    actualHour: 400,
    description: "客户需要将客户数据导出为Excel格式，支持多种筛选条件",
    attachments: [],
    tags: ["导出", "Excel"]
  },
  {
    id: uuidv4(),
    name: "20220709性能优化",
    type: "性能问题",
    planHour: 600,
    cost: "￥8,0000",
    charge: "是",
    remark: "页面加载速度过慢，需要优化",
    creator: "赵六",
    createTime: "2022/06/03",
    updateTime: "2022/06/03",
    projectId: "P123123231",
    projectNumber: "P123123231",
    projectName: "CRM系统",
    status: "已完成",
    priority: "中",
    assignee: "钱七",
    actualHour: 550,
    description: "客户列表页面加载时间超过5秒，需要优化数据库查询和前端渲染",
    attachments: [],
    tags: ["性能", "优化"]
  },
  {
    id: uuidv4(),
    name: "20220710兼容性修复",
    type: "兼容性问题",
    planHour: 400,
    cost: "￥5,0000",
    charge: "否",
    remark: "在IE浏览器中显示异常",
    creator: "孙八",
    createTime: "2022/06/04",
    updateTime: "2022/06/04",
    projectId: "P123123231",
    projectNumber: "P123123231",
    projectName: "CRM系统",
    status: "已关闭",
    priority: "低",
    assignee: "周九",
    actualHour: 380,
    description: "系统在IE11浏览器中部分功能无法正常使用，需要修复兼容性问题",
    attachments: [],
    tags: ["兼容性", "IE"]
  },
  {
    id: uuidv4(),
    name: "20220711报表功能增强",
    type: "优化需求",
    planHour: 1000,
    cost: "￥12,0000",
    charge: "是",
    remark: "增加更多图表类型和自定义选项",
    creator: "吴十",
    createTime: "2022/06/05",
    updateTime: "2022/06/05",
    projectId: "P123123231",
    projectNumber: "P123123231",
    projectName: "CRM系统",
    status: "处理中",
    priority: "中",
    assignee: "郑十一",
    actualHour: 300,
    description: "客户希望报表功能支持更多图表类型，如饼图、柱状图等，并支持自定义颜色和样式",
    attachments: [],
    tags: ["报表", "图表"]
  },
  {
    id: uuidv4(),
    name: "20220712移动端适配",
    type: "功能需求",
    planHour: 1500,
    cost: "￥20,0000",
    charge: "是",
    remark: "开发移动端版本",
    creator: "王十二",
    createTime: "2022/06/06",
    updateTime: "2022/06/06",
    projectId: "P123123231",
    projectNumber: "P123123231",
    projectName: "CRM系统",
    status: "待处理",
    priority: "高",
    assignee: "李十三",
    actualHour: 0,
    description: "客户要求开发移动端版本，支持手机和平板设备访问，需要响应式设计",
    attachments: [],
    tags: ["移动端", "响应式"]
  }
];

// 初始化数据
async function initData() {
  try {
    // 清空现有数据
    await afterSalesDemandModel.deleteMany({});
    console.log('已清空现有数据');

    // 插入测试数据
    const result = await afterSalesDemandModel.insertMany(testData);
    console.log(`成功插入 ${result.length} 条测试数据`);

    // 验证数据
    const count = await afterSalesDemandModel.countDocuments();
    console.log(`数据库中总共有 ${count} 条记录`);

    console.log('数据初始化完成！');
  } catch (error) {
    console.error('数据初始化失败:', error);
  } finally {
    mongoose.connection.close();
  }
}

// 运行初始化
initData(); 